package com.besall.allbase.view.activity.chipstoollevel4.protocol.InfoParams;

import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_FORMAT_CHECK;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;

public class TimeFormat extends DialogFragment {
        public String mFormat = "0";

    private ConfigCallBack mConfigCallback;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_timeformat, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){

        RadioGroup chooseFormat = view.findViewById(R.id.timeformat);
        chooseFormat.setOnCheckedChangeListener(FormatCheckChangeListener);

        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);

    }

    public boolean saveConfig(){
        SPHelper.putPreference(getActivity(), KEY_FORMAT_CHECK, mFormat);
        return true;
    }

    private final RadioGroup.OnCheckedChangeListener FormatCheckChangeListener = (group, checkedId) -> {

        if(checkedId == R.id.half){

            mFormat = "1";
        }else if(checkedId == R.id.full){

            mFormat = "0";
        }

/*         switch (checkedId){
            case R.id.half:
                mFormat = "1";
                break;
            case R.id.full:
                mFormat = "0";
                break;
        }*/
    };

    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };


    public interface ConfigCallBack {

        void onConfigOk();

        void onConfigCancel();
    }



}
